*******************************************************************************
* Load data
*******************************************************************************

use dataset_gss, clear

*******************************************************************************
* Loop over subgroups
*******************************************************************************

foreach subgroup in All Democrat Republican {
	
*******************************************************************************
* Keep the subgroup
*******************************************************************************

preserve

keep if subgroup == "`subgroup'"

*******************************************************************************
* Label lines if it's the first plot
*******************************************************************************

local legend1 legend(off)
local legend2 legend(off)
if "`subgroup'" == "All" {
	local legend1 legend(order(1 ///
		"{fontface monospace:{bf:Support Police Spending}}" ///
		2 "{fontface monospace:{bf:Synthetic Control}}") ///
		symysize(0) symxsize(0) ring(0) pos(11) cols(1) ///
		region(lwid(none) fcol(none)))
	local legend2 legend(order(1 "{fontface monospace:{bf:Difference}}") ///
		symysize(0) symxsize(0) ring(0) pos(11) cols(1) ///
		region(lwid(none)))
	
}

*******************************************************************************
* Make lags
*******************************************************************************

local lags
levelsof year if year < 2014, local(levels)
foreach i of local levels {
	egen nat`i'm = mean(nat) if year == `i', by(target)
	egen nat`i' = mean(nat`i'm), by(target)
	drop nat`i'm
	local lags `lags' nat`i'
}

*******************************************************************************
* Set panel and get the treated ID
*******************************************************************************

encode target, gen(id)
xtset id year
su id if target == "crimy"

*******************************************************************************
* Synth
*******************************************************************************

synth nat `lags', trunit(`r(mean)') trperiod(2014) ///
	keep(tmp`subgroup', replace)

*******************************************************************************
* Save the unweighted data for now
*******************************************************************************

save tmp, replace

*******************************************************************************
* Read in the weights for the "all" subgroup
*******************************************************************************

use tmpAll, clear
rename _W_ wtall
keep _Co_ wtall

*******************************************************************************
* Merge with weights for this subgroup
*******************************************************************************

drop if mi(_Co_)
merge 1:m _Co using tmp`subgroup'
drop if mi(_Co)
keep if _m == 3
rename _W_ wtsub

*******************************************************************************
* Merge back with unweighted data
*******************************************************************************

keep _Co_ wtall wtsub
decode _Co_, gen(target)
merge 1:m target using tmp
erase tmp.dta

*******************************************************************************
* Make the SC's
*******************************************************************************

keep target year wtall wtsub nat
reshape i year
reshape j target, string
reshape xi 
reshape xij nat wtall wtsub
reshape wide

local targets aid aidy arms armsy city cityy crime crimy ///
	drug drugy educ educy envir enviy fare farey heal ///
	healy mass park race racey road soc spac spacy

local wtds
foreach l in `targets' {
	gen wtd`l'alt = wtall`l'*nat`l'
	local wtds `wtds' wtd`l'alt
}
egen _Y_synthetic_alt = rowtotal(`wtds')

local wtds
foreach l in `targets' {
	gen wtd`l' = wtsub`l'*nat`l'
	local wtds `wtds' wtd`l'
}
egen _Y_synthetic = rowtotal(`wtds')

rename natcrimy _Y_treated 
rename year _time

*******************************************************************************
* Graph
*******************************************************************************

#delimit;

gr tw
	(line _Y_treated _time, col(black) lwid(thick))
	(line _Y_synthetic _time, col(gray) lwid(thick))
	(line _Y_synthetic_alt _time, col(red) lpat(solid))
	,
		name(g1, replace)
		xline(2014, lpat(dash))
		ylab(0.2(0.2)0.8, angle(horiz))
		plotregion(style(none))
		ytitle("")
		xtitle("")
		`legend1'	
		xlab(1990(8)2022)		
		;

#delimit cr

gr_edit .legend.plotregion1.label[2].style.editstyle color(gray) editcopy

gen d = _Y_treated - _Y_synthetic
gen d_alt = _Y_treated - _Y_synthetic_alt

#delimit;

gr tw
	(line d _time, col(black) lwid(thick))
	(line d_alt _time, col(red) lpat(solid))
	(sc d _time, msym(none))
	,
		name(g2, replace)
		yline(0, lpat(shortdash))
		xline(2014, lpat(dash))
		plotregion(style(none))
		ylab(-.3(0.2)0.5, angle(horiz))
		ytitle("")
		xtitle("")
		xlab(1990(8)2022)		
		`legend2'
		;

#delimit cr

local mytitle `subgroup'
if "`subgroup'" != "All" local mytitle = "`subgroup's" 

gr combine g1 g2, name(G`subgroup', replace) title(`mytitle')

*******************************************************************************
* Close subgroup loop
*******************************************************************************

restore

}

*******************************************************************************
* Combine graphs
*******************************************************************************

gr combine GAll GDemocrat GRepublican, rows(3) ysize(4) xsize(3) ///
	imargin(zero)

*******************************************************************************
* Clean up
*******************************************************************************

erase tmpAll.dta
erase tmpDemocrat.dta
erase tmpRepublican.dta

*******************************************************************************
* Save
*******************************************************************************

gr export "figure_sc_sameweights_gss.pdf", replace

*******************************************************************************
* End
*******************************************************************************
